/**
 * Form extensions & customizations for ExtJS
 * 
 * @author Max Romanovsky
 */
Ext.ns('Ext.ux.Bike.form');

/**
 * Dictionary field
 * 
 * @class Ext.ux.Bike.form.DictionaryField
 * @extends Ext.form.ComboBox
 */
Ext.ux.Bike.form.DictionaryField = Ext.extend(Ext.form.ComboBox, {
			forceSelection : true,
			typeAhead : false,
			editable : false,
			triggerAction : 'all',
			valueField : 'id',
			displayField : 'title',
			selectOnFocus : true,
			emptyText : ''
		});

Ext.reg('dictfield', Ext.ux.Bike.form.DictionaryField);

/**
 * TextEditor field
 * 
 * @class Ext.ux.Bike.form.TextEditorField
 * @extends Ext.form.TriggerField
 */
Ext.ux.Bike.form.TextEditorField = Ext.extend(Ext.form.TriggerField, {
			windowTitle : 'Text editor',
			saveText : 'Save',
			cancelText : 'Cancel',
			triggerClass : 'x-form-trigger-add',
			editable : false,
			value : null,

			initComponent : function() {
				Ext.ux.Bike.form.TextEditorField.superclass.initComponent.call(this);
				this.on('specialkey', function(f, e) {
							if (e.getKey() == e.ENTER) {
								this.onTriggerClick();
							}
						}, this);
				this.getEditorForm();
			},

			setValue : function(v) {
				this.value = v;
				this.setRawValue(v);
			},

			getValue : function() {
				return this.value;
			},

			onTriggerClick : function() {
				this.onFocus();
				this.getEditorForm().show();
				this.getEditorForm().getEditor().setValue(this.getValue());
				this.getEditorForm().getEditor().focus();
			},

			validateBlur : function() {
				return !this.frm || !this.frm.isVisible();
			},

			getEditorForm : function() {
				if (!this.frm) {
					this.editr = new Ext.form.TextArea({});

					this.frm = new Ext.Window({
								title : this.windowTitle,
								iconCls : 'icon-edit',
								width : 500,
								height : 300,
								closeAction : 'hide',
								layout : 'fit',
								maximizable : true,
								modal : true,
								items : [this.editr],
								buttons : [{
											text : this.saveText,
											handler : function() {
												this.setValue(this.editr.getValue());
												this.frm.hide();
											}.createDelegate(this)
										}, {
											text : this.cancelText,
											handler : function() {
												this.frm.hide();
											}.createDelegate(this)
										}],
								getEditor : function() {
									return this.editr;
								}.createDelegate(this)
							});
				}
				return this.frm;
			}
		});

Ext.reg('texteditorfield', Ext.ux.Bike.form.TextEditorField);

/**
 * HtmlEditor field
 * 
 * @class Ext.ux.Bike.form.HtmlEditorField
 * @extends Ext.form.TriggerField
 */
Ext.ux.Bike.form.HtmlEditorField = Ext.extend(Ext.form.TriggerField, {
			windowTitle : 'HTML editor',
			saveText : 'Save',
			cancelText : 'Cancel',
			triggerClass : 'x-form-trigger-add',
			editable : false,
			value : null,
			tinymceSettings : null,
			tinymceSettingsMerge : false,

			initComponent : function() {
				Ext.ux.Bike.form.HtmlEditorField.superclass.initComponent.call(this);

				if (!Ext.isEmpty(this.tinymceSettings)
						&& this.tinymceSettingsMerge) {
					var tinymceSettings = Ext.ux.Bike.tinymce.settings;
					Ext.apply(tinymceSettings, this.tinymceSettings);
					this.tinymceSettings = tinymceSettings;
				} else if (Ext.isEmpty(this.tinymceSettings)) {
					this.tinymceSettings = Ext.ux.Bike.tinymce.settings;
				}

				this.on('specialkey', function(f, e) {
							if (e.getKey() == e.ENTER) {
								this.onTriggerClick();
							}
						}, this);
				this.getEditorForm();
			},

			setValue : function(v) {
				this.value = v;
				this.setRawValue(v);
			},

			getValue : function() {
				return this.value;
			},

			onTriggerClick : function() {
				this.onFocus();
				this.getEditorForm().show();
				this.getEditorForm().getEditor().setValue(this.getValue());
				this.getEditorForm().getEditor().focus();
			},

			validateBlur : function() {
				return !this.frm || !this.frm.isVisible();
			},

			getEditorForm : function() {
				if (!this.frm) {
					// this.editr = new Ext.form.HtmlEditor({});
					this.editr = new Ext.ux.TinyMCE({
								tinymceSettings : this.tinymceSettings
							});

					this.frm = new Ext.Window({
								title : this.windowTitle,
								iconCls : 'icon-edit',
								width : 600,
								height : 400,
								closeAction : 'hide',
								layout : 'fit',
								maximizable : true,
								modal : true,
								items : [this.editr],
								buttons : [{
											text : this.saveText,
											handler : function() {
												this.setValue(this.editr.getValue());
												this.frm.hide();
											}.createDelegate(this)
										}, {
											text : this.cancelText,
											handler : function() {
												this.frm.hide();
											}.createDelegate(this)
										}],
								getEditor : function() {
									return this.editr;
								}.createDelegate(this)
							});
				}
				return this.frm;
			}
		});
Ext.reg('htmleditorfield', Ext.ux.Bike.form.HtmlEditorField);

/**
 * FileBrowser field
 * 
 * @class Ext.ux.Bike.form.FileBrowserField
 * @extends Ext.form.TriggerField
 */
Ext.ux.Bike.form.FileBrowserField = Ext.extend(Ext.form.TriggerField, {
			windowTitle : 'File browser',
			triggerClass : 'x-form-trigger-add',
			folderRoot : '/uploads',
			actionURL : '/Bike/controllers/filebrowser.php',
			editable : false,
			value : null,

			initComponent : function() {
				Ext.ux.Bike.form.FileBrowserField.superclass.initComponent.call(this);
				this.on('specialkey', function(f, e) {
							if (e.getKey() == e.ENTER) {
								this.onTriggerClick();
							}
						}, this);
				this.fb = new Ext.ux.fileBrowser({
							folderRoot : this.folderRoot,
							actionURL : this.actionURL,
							modal : true,
							handler : function(filename) {
								this.setValue(filename);
								this.fb.hide();
							}.createDelegate(this),
							statusBar : true,
							title : this.windowTitle
						});
			},

			setValue : function(v) {
				this.value = v;
				this.setRawValue(v);
			},

			getValue : function() {
				return this.value;
			},

			onTriggerClick : function() {
				this.onFocus();
				this.fb.show();
				this.fb.setValue(this.getValue());
			},

			validateBlur : function() {
				return !this.fb || !this.fb.isVisible();
			}
		});
Ext.reg('filebrowserfield', Ext.ux.Bike.form.FileBrowserField);

/**
 * ImageBrowser field
 * 
 * @class Ext.ux.Bike.form.ImageBrowserField
 * @extends Ext.form.TriggerField
 */
Ext.ux.Bike.form.ImageBrowserField = Ext.extend(Ext.form.TriggerField, {
			windowTitle : 'Image browser',
			triggerClass : 'x-form-trigger-add',
			folderRoot : '/uploads',
			actionURL : '/Bike/controllers/filebrowser.php',
			editable : false,
			value : null,

			initComponent : function() {
				Ext.ux.Bike.form.ImageBrowserField.superclass.initComponent.call(this);
				this.on('specialkey', function(f, e) {
							if (e.getKey() == e.ENTER) {
								this.onTriggerClick();
							}
						}, this);
				this.fb = new Ext.ux.fileBrowser({
							imageBrowser : true,
							folderRoot : this.folderRoot,
							actionURL : this.actionURL,
							modal : true,
							handler : function(filename) {
								this.setValue(filename);
								this.fb.hide();
							}.createDelegate(this),
							statusBar : true,
							title : this.windowTitle
						});
			},

			setValue : function(v) {
				this.value = v;
				this.setRawValue(v);
			},

			getValue : function() {
				return this.value;
			},

			onTriggerClick : function() {
				this.onFocus();
				this.fb.show();
				this.fb.setValue(this.getValue());
			},

			validateBlur : function() {
				return !this.fb || !this.fb.isVisible();
			}
		});
Ext.reg('imagebrowserfield', Ext.ux.Bike.form.ImageBrowserField);